//
// Created by xili on 2024/8/31 14:01.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int numRabbits(vector<int> &answers) {
        sort(answers.begin(), answers.end()); // 0 0 1 2 2
        int ans = 0;
        int n = answers.size();
        int cur = -1;
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            if (cur != answers[i] || cnt == cur + 1) {
                cur = answers[i];
                ans += cur+1;
                cnt = 1;
            } else {
                cnt++;
            }
        }
        ans += cur + 1;
        return ans;
    }
};